package com.idp.web.test.service.impl;

import com.idp.web.test.service.ProductTypeService;
import com.idp.web.system.entity.SysOrg;
import com.idp.web.test.dao.ProductTypeDao;
import com.idp.web.test.entity.ProductType;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.idp.common.persistence.Page;
import com.idp.common.util.ResourceUtils;

/**
 * 
 * 产品类型service实现类
 * 
 * <pre>
 * 	历史记录：
 * 	2017-06-02 Amber
 * 	新建文件
 * </pre>
 * 
 * @author
 * 
 * 		<pre>
 * SD
 * 	Amber
 * PG
 *	Amber
 * UT
 *
 * MA
 *         </pre>
 * 
 * @version $Rev$
 *
 *          <p/>
 *          $Id$
 *
 */
@Service("productTypeService")
public class ProductTypeServiceImpl implements ProductTypeService {

	@Resource
	private ProductTypeDao productTypeDao;

	@Override
	public Page<ProductType> findByPage(ProductType productType, Page<ProductType> page) {
		// TODO Auto-generated method stub
		page.setResult(productTypeDao.find(productType, page));
		return page;
	}

	@Override
	public List<ProductType> findForTreeTable(Integer parent) {
		return productTypeDao.findForTreeTable(parent);
	}

	@Override
	public List<ProductType> findBySearch(ProductType productType) {

		return productTypeDao.find(productType);
	}

	@Override
	public ProductType getById(Integer id) {

		return productTypeDao.getById(id);
	}

	@Override
	public void add(ProductType productType) {

		productTypeDao.add(productType);
	}

	@Override
	public void update(ProductType productType) {

		productTypeDao.update(productType);
	}

	@Override
	public void delete(Integer id) {

		productTypeDao.delete(id);
	}

	@Override
	public List<ProductType> findForTreeTableNew() {
		List<ProductType> productTypes = productTypeDao.find(null);
		List<ProductType> result = new ArrayList<>();
		for (ProductType pt : productTypes) {
			ProductType p = new ProductType();
			p.setParent(pt.getId());
			List<ProductType> childs = productTypeDao.find(p);
			if (childs != null) {
				pt.setChildList(childs);
				pt.setChildCount(childs.size());
			}
			if(pt.getParent() == 0) {
				result.add(pt);
			}
		}
		return result;
	}

}